package com.example.xiaoweiassistant.Activity.IndexActivity;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import com.example.xiaoweiassistant.Fragment.indexFragment;
import com.example.xiaoweiassistant.R;
import com.google.gson.Gson;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class IndexActivity extends AppCompatActivity{

    private static final String TAG = "IndexActivity";
    private ImageView imageView;
    private  boolean pop_out = false ;
    private AppCompatActivity _this;

    PopupWindow newPopWid;

    public static  String model = "" ;

    private  EventManager asr;

    private boolean logTime = true;

    protected   boolean enableOffline = false; // 测试离线命令词，需要改成true

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 100){
                newPopWid.dismiss();
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        init();
    }

    private void init() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add( R.id.fragment  ,new indexFragment());
        fragmentTransaction.commit() ;

        imageView = findViewById(R.id.yuying);

        imageView.setOnLongClickListener(new View.OnLongClickListener() {

            private PopupWindow popupWindow;



            @Override
            public boolean onLongClick(View v) {

                if(!pop_out){
                    popupWindow = new PopupWindow(  LayoutInflater.from(IndexActivity.this).inflate(R.layout.yuying_pop , null , true) , 600 , 400 );

                    popupWindow.showAsDropDown(
                            LayoutInflater.from(IndexActivity.this).inflate(R.layout.yuying_pop , null , false),
                            270,
                            1000);
                }else {
                    popupWindow.dismiss();
                     newPopWid = new PopupWindow(LayoutInflater.from(IndexActivity.this).inflate(R.layout.loading, null, true), 400, 400);
                    newPopWid.showAsDropDown(
                            LayoutInflater.from(IndexActivity.this).inflate(R.layout.loading , null , false),
                            270,
                            1000);

                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            Message obtain = Message.obtain();
                            obtain.what = 100 ;
                            handler.sendMessage(obtain);
                        }
                    } , 2000 , 1000);



                }

                pop_out = !pop_out;
                return true;
            }
        });

    }




}